<script type="text/javascript">
/* <![CDATA[ */ 
//Global Variables
var isAppletLoaded = false;
var isFriendsDataLoaded = false;
var isMessageDataLoaded = false;
var isPostDataLoaded = false;
var isCommentDataLoaded = false;
var isLikeDataLoaded = false;

var threshold = 0;
var maxScore = 100;
var maxDistance = 250;

$(document).ready(function(){
	
    [##_AJAX_FUNCTION_##]
	
    $('#refresh-image').hide();
    $('#threshold').val(threshold);
    $('#slider').slider({
        slide: function(event,ui){
            threshold = ui.value;
            $('#threshold').val(threshold);
            drawGraph();
        }
    });

var dWidth = 320;
var dHeight = 400;

shimIFrame();
$('#friend-container').dialog({ 
    autoOpen: false,
    width: 300, 
    height: 270,
    position: [300, 300],
    open: function(event, ui){
		
        $('#shimmer')
            .css('left', $('.ui-dialog').css('left'))
            .css('top' , $('.ui-dialog').css('top'))
            .width($('.ui-dialog').width()+8)
            .height($('.ui-dialog').height()+8)
            .show();
    },
    drag: function(event, ui){
        $('#shimmer')
            .css('left', ui.position.left)
            .css('top', ui.position.top)
    },
    resize: function(event, ui){
        $('#shimmer')
            .css('left', ui.position.left)
            .css('top' , ui.position.top)
            .width($('.ui-dialog').width()+8)
            .height($('.ui-dialog').height()+8);
    },
    close: function(event, ui){
        $('#shimmer').hide();
    }
  });   
    
    //Button Event Handlers
    $('#refresh-image').click(drawGraph);
    $('#remove_zero_score').change(drawGraph);
    $('#show_friend_table').click(function(){
        $('#friend-container').dialog("open");
    });
	$('#friends_of_friends').click(function(){
		getFriendsOfFriendsAJAX();
	});

});

function drawGraph(){

    if(isAppletLoaded && isFriendsDataLoaded){

	    //sort uing tablesorter http://tablesorter.com
	    $("table.rankTable").tablesorter({ 
	        // sort on the third column, order dsc 
	        sortList: [[2,1]] 
	    });
		
		$("table.rankTable tbody tr").each(function(index, row){
			var cols = $(row).children();
			cols.eq(0).text(index+1);
		});

        var obj = document.getElementById('friendsNet');
		
		//show graph
		obj.setAttribute("style", "visibility:visible;");
        
        if (obj != null) {
            obj.setMaxDistance(maxDistance);
            obj.clearGraph();
    
            var maxValue = 100;
            var friends = $('table.rankTable');
            $('tr', friends).each(function(index){
                if (index != 0) {
                    var cols = $(this).children();
                    
                    if (index == 1 && (cols.eq(2).text() != 0)) {
                        maxValue = cols.eq(2).text();
						//give slider max value
                        $('#slider').slider("option", "max", maxValue);
                    }
                    fId = $(cols.eq(1)).attr('id');
                    name = cols.eq(1).text();
                    score = cols.eq(2).text();
    
                    if(score >= threshold){
                        obj.addFriend(fId, name, getDistance(score, maxValue));
                    }
    
                }
            });
        }
    }
}

function getFriendsAJAX(){
	
	$('.subtitle').show();
    $('table.rankTable tbody').empty();
	var applet = document.getElementById('friendsNet');
	applet.setAttribute("style", "visibility:hidden");
	$('#loading-box').html("Loading friends data").show();
	
	if ($.data(document.body, 'friends') != null) {
		
		//hide loading image
        $('#loading-box').hide();

        //Build Friend Table
        buildFriendTable($.data(document.body, 'friends'));
    
	   //onLoad Event Handler		
        onFriendsDataLoaded();
		
	} else {
			
		$.getJSON('/o-my-friends/ajax/getFriendsAJAX.php?[##_QUERY_STRING_##]', null, function(data){

            //hide loading image
			$('#loading-box').hide();

			//check if there is an error
			if(data.error != null){
				if(confirm(data.error + "\nPress OK to try again")){
                    getFriendsAJAX();					
				}
            }else{
			
				//store data 
				$.data(document.body, 'friends', data);
				
				//Build Friend Table
				buildFriendTable(data);

                //onLoad Event Handler				
				onFriendsDataLoaded();
				
            }				
		});
	}        
}

function getMessageRankAJAX(){
	$('#message').html("Processing Message Data").show();
	if ($.data(document.body, 'message') != null) {
		//extract data
		data = $.data(document.body, 'message');
			   
		//Build Friend Table
		addToFriendTable(data);
		
		//hide loading image
        $('#message').empty().hide();
		
		//Call onLoad Event Handler
		onMessageDataLoaded();
	}
	else {
		$.getJSON('/o-my-friends/ajax/getFriendsDataAJAX.php?mode=1&[##_QUERY_STRING_##]', null, function(data){
		
			//hide loading image
			$('#message').empty().hide();
			
			//check if there is an error
			if (data.error != null) {
				if (confirm(data.error + "\nPress OK to try again")) {
					getMessageRankAJAX();
				}
			}
			else {
			
				//store data 
				$.data(document.body, 'message', data);
				
				//Build Friend Table
				addToFriendTable(data);

                //onLoad Event Handler			
				onMessageDataLoaded();
			}
		});
	}	
}

function getPostRankAJAX(){
    $('#message').html("Processing Post Data").show();
    if ($.data(document.body, 'post') != null) {
		//extract data
		data = $.data(document.body, 'post');
		
		//Build Friend Table
		addToFriendTable(data);
		
		//hide loading image
        $('#message').empty().hide();
		
		//Call onLoad Event Handler
		onPostDataLoaded();
	}
	else {
		$.getJSON('/o-my-friends/ajax/getFriendsDataAJAX.php?mode=2&[##_QUERY_STRING_##]', null, function(data){
		
			//hide loading image
			$('#message').empty().hide();
			
			//check if there is an error
			if (data.error != null) {
				if (confirm(data.error + "\nPress OK to try again")) {
					getPostRankAJAX();
				}
			}
			else {
			
				//store data 
				$.data(document.body, 'post', data);
				
				//Build Friend Table
				addToFriendTable(data);

                //onLoad Event Handler				
				onPostDataLoaded();
				
			}
		});
	}
}

function getCommentRankAJAX(){

    $('#message').html("Processing Comment Data").show();
	if ($.data(document.body, 'comment') != null) {
		//extract data
		data = $.data(document.body, 'comment');
		
		//Build Friend Table
		addToFriendTable(data);
		
		//hide loading image
        $('#message').empty().hide();
		
		//Call onLoad Event Handler
		onCommentDataLoaded();
	}
	else {
		$.getJSON('/o-my-friends/ajax/getFriendsDataAJAX.php?mode=4&[##_QUERY_STRING_##]', null, function(data){
		
			//hide loading image
			$('#message').empty().hide();
			
			//check if there is an error
			if (data.error != null) {
				if (confirm(data.error + "\nPress OK to try again")) {
					getCommentRankAJAX();
				}
			}
			else {
			
				//store data 
				$.data(document.body, 'comment', data);
				
				//Build Friend Table
				addToFriendTable(data);

                //onLoad Event Handler				
				onCommentDataLoaded();
				
			}
		});
	}
}

function getLikeRankAJAX(){
    $('#message').html("Processing Like Data").show();
	if ($.data(document.body, 'like') != null) {
	    data = $.data(document.body, 'like');
	   
	    //Build Friend Table
	    addToFriendTable(data);
		
		//hide loading image
		$('#message').empty().hide();
		
	    //onLoad Event Handler
	    onLikeDataLoaded();
	}
	else {
		$.getJSON('/o-my-friends/ajax/getFriendsDataAJAX.php?mode=8&[##_QUERY_STRING_##]', null, function(data){
		
			//hide loading image
			$('#message').empty().hide();
			
			//check if there is an error
			if (data.error != null) {
				if (confirm(data.error + "\nPress OK to try again")) {
					getLikeRankAJAX();
				}
			}
			else {
			
				//store data 
				$.data(document.body, 'like', data);
				
				//Build Friend Table
				addToFriendTable(data);
				
                //onLoad Event Handler
				onLikeDataLoaded();
				
			}
		});
	}
}

function getFriendsOfFriendsAJAX(){

    if ($.data(document.body, 'friendsOfFriends') != null) {
        
        //hide loading image
        $('#loading-box').hide();
        
        addRelations($.data(document.body, 'friendsOfFriends'));
                        
    } else {
            
        $.getJSON('/o-my-friends/ajax/getFriendsOfFriendsAJAX.php?[##_QUERY_STRING_##]', null, function(data){

            //hide loading image
            $('#loading-box').hide();

            //check if there is an error
            if(data.error != null){
                alert(data.error);
            }else{
            
                //store data 
                $.data(document.body, 'friendsOfFriends', data);

                addRelations(data);
                
            }               
        });
    }
}

function addRelations(data){
    var applet = document.getElementById('friendsNet');
	applet.initFriendsOfFriends();
    $(data).each(function(index, item){
        //Spring makeSpring( Particle a, Particle b, float strength, float damping, float restLength )
        applet.addRelation(item.id1, item.id2, 0.01, 0.01, 100);
    });
}

function buildFriendTable(data){
    var tbody = $('table.rankTable tbody');
    $(data).each(function(item, value){
		
		if(item == 0 && (value.score != 0)){
			maxScore = value.score;
		}
		
        tbody.append("<tr>" + 
                          "<td>"+ eval(item + 1) +"</td>" + 
                          "<td id='"+ value.fId +"'><a target='_blank' href='[##_MY_LINK_URL_##]and=" + value.fId + "'>" + value.name +"</td>" + 
                          "<td>" + value.score + "</td>" +
                    "</tr>");
    });
}

function addToFriendTable(data){
	var applet = document.getElementById('friendsNet');
    var tbody = $('table.rankTable tbody');
	$(data).each(function(index, item){
		var score = ($('#'+item.fId).next().text()-0) + (item.score-0);
		$('#'+item.fId).next().text(score);
	});
}

function onAppletLoaded(){
    isAppletLoaded = true;
    drawGraph();
}

function onFriendsDataLoaded(){
    isFriendsDataLoaded = true;
    drawGraph();	
	getMessageRankAJAX();
}

function onMessageDataLoaded(){
    isMessageDataLoaded = true;
    drawGraph();
	getPostRankAJAX();
}

function onPostDataLoaded(){
    isPostDataLoaded = true;
	drawGraph();
	getCommentRankAJAX();
}

function onCommentDataLoaded(){
    isCommentDataLoaded = true;
	drawGraph();
	getLikeRankAJAX();
}

function onLikeDataLoaded(){
    isLikeDataLoaded = true;
	drawGraph();
	$('#refresh-image').show();
}

function linkToFriendshipPage(id){
	var url = "/o-my-friends/friendship.php?[##_QUERY_STRING_##]&fId=" + id;
	if (confirm("Do you want to go to the friendship page?")) {
	   window.location.href = url;
    }
}

/* ]]> */
</script>